﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

public partial class insurance_orderconfirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        switch (RequestBase.GetFormString("___EVENTTARGET"))
        {
            case "saveinfo":
                SaveOrder(1);
                break;
            case "saveandgotopayment":
                SaveOrder(2);
                break;
        }

        if (!IsPostBack)
        {
            InitControlValuesByPost();
        } 
    }

    #region 获取POST值初始化控件
    void InitControlValuesByPost()
    {
        #region 值
        txtProductItemId.Value = RequestBase.GetFormString("txtProductItemId");
        txtProductId.Value = RequestBase.GetFormString("selProductList");
        txtProductName.Value = RequestBase.GetFormString("txtProductName");
        txtProductItemName.Value = RequestBase.GetFormString("txtProductItemName");
        txtPassCountryCode.Value = RequestBase.GetFormString("txtPassCountryCode");
        txtPassCountry.Value = RequestBase.GetFormString("txtPassCountry");
        txtDestCountryCode.Value = RequestBase.GetFormString("selDestinationList");
        txtDestCountryName.Value = RequestBase.GetFormString("selDestinationList");
        txtPhone.Value = RequestBase.GetFormString("txtPhone");
        txtCJType.Value = RequestBase.GetFormString("ddPolicyType");
        txtEmail.Value = RequestBase.GetFormString("txtEmail");
        txtStartDate.Value = RequestBase.GetFormString("txtStartDate");
        txtEndDate.Value = RequestBase.GetFormString("txtEndDate");

        txtTName.Value = RequestBase.GetFormString("txtTName");
        txtTNameEn.Value = RequestBase.GetFormString("txtTNameEn");
        txtTBirthday.Value = RequestBase.GetFormString("txtTBirthday");
        txtTSex.Value = RequestBase.GetFormString("selTSex");
        txtTIDType.Value = RequestBase.GetFormString("selTIDType");
        txtTIDNo.Value = RequestBase.GetFormString("txtTIDNo");
        txtTAge.Value = RequestBase.GetFormString("txtTAge");
        
        txtPrice.Value = RequestBase.GetFormString("txtPrice");

        txtName.Value = RequestBase.GetFormString("txtName");
        txtNameEn.Value = RequestBase.GetFormString("txtNameEn");
        txtIDType.Value = RequestBase.GetFormString("selIDType");
        txtBirthday.Value = RequestBase.GetFormString("txtBirthday");
        txtSex.Value = RequestBase.GetFormString("selSex"); 
        txtIDNo.Value = RequestBase.GetFormString("txtIDNo");
        txtAge.Value = RequestBase.GetFormString("txtAge");

        #region 受益人
        //txtSIDNo.Value = RequestBase.GetFormString("txtSIDNo");
        //txtSIDType.Value = RequestBase.GetFormString("selSIDType");
        //txtSRelation.Value = RequestBase.GetFormString("selSRelation");
        //txtSNameEn.Value = RequestBase.GetFormString("txtSNameEn");
        //txtSName.Value = RequestBase.GetFormString("txtSName");
        //txtSBnfPortion.Value = RequestBase.GetFormString("txtSBnfPortion");
        //txtSBnforder.Value = RequestBase.GetFormString("txtSBnforder");
        #endregion

        txtSalesTypeCode.Value = RequestBase.GetFormString("rdoSalesTypeCode");
        txtExpressType.Value = RequestBase.GetFormString("selExpressType");
        txtInvoice.Value = RequestBase.GetFormString("rdoInvoice");
        txtInvoiceRecipient.Value = RequestBase.GetFormString("txtInvoiceRecipient");
        txtInvoicePhone.Value = RequestBase.GetFormString("txtInvoicePhone");
        txtInvoiceTitle.Value = RequestBase.GetFormString("txtInvoiceTitle");
        txtInvoiceAddress.Value = RequestBase.GetFormString("txtInvoiceAddress");

        /// 需要发票或纸质保单
        if (txtInvoice.Value == "1" || txtSalesTypeCode.Value.Trim() == "2")
            txtInvoiceFee.Value = txtExpressType.Value.Replace("顺丰快递", "20.00").Replace("申通快递", "10.00");
        else
            txtInvoiceFee.Value = "0";
        #endregion

        #region 显示
        lblProductName.Text = RequestBase.GetFormString("txtProductName");
        lblProductItemName.Text = RequestBase.GetFormString("txtProductItemName");
        lblEmail.Text = RequestBase.GetFormString("txtEmail");
        lblPhone.Text = RequestBase.GetFormString("txtPhone");
        lblPrice.Text = RequestBase.GetFormString("txtPrice");
        lblStartDate.Text = RequestBase.GetFormString("txtStartDate");
        lblEndDate.Text = RequestBase.GetFormString("txtEndDate");
        lblDestination.Text = BLL.Sys.EnumItem.GetTextByValue("DestCountryName", RequestBase.GetFormString("selDestinationList")); 
        lblPlicyType.Text = BLL.Sys.EnumItem.GetTextByValue("CJType", RequestBase.GetFormString("ddPolicyType")); 

        lblTName.Text = RequestBase.GetFormString("txtTName");
        lblTNameEn.Text = RequestBase.GetFormString("txtTNameEn");
        lblTBirthday.Text = RequestBase.GetFormString("txtTBirthday");
        lblTSex.Text = RequestBase.GetFormString("selTSex").Replace("1", "女").Replace("0", "男");
        lblTIDType.Text = BLL.Sys.EnumItem.GetTextByValue("IDType", RequestBase.GetFormString("selTIDType"));
        lblTIDNo.Text = RequestBase.GetFormString("txtTIDNo");
        lblTAge.Text = RequestBase.GetFormString("txtTAge");

        lblInvoiceFee.Text = txtInvoiceFee.Value;
        lblExpressType.Text = RequestBase.GetFormString("selExpressType");
        lblSalesTypeCode.Text = RequestBase.GetFormString("rdoSalesTypeCode").Replace("1", "电子保单(PDF)").Replace("2", "纸质保单");
        lblInvoice.Text = RequestBase.GetFormString("rdoInvoice").Replace("1", "是").Replace("0", "否");
        lblInvoiceRecipient.Text = RequestBase.GetFormString("txtInvoiceRecipient");
        lblInvoicePhone.Text = RequestBase.GetFormString("txtInvoicePhone");
        lblInvoiceTitle.Text = RequestBase.GetFormString("txtInvoiceTitle");
        lblInvoiceAddress.Text = RequestBase.GetFormString("txtInvoiceAddress");

        PolicyholdersListDataBind();

        lblPersonQuantity.Text = txtPersonQuantity.Value;
        lblTotalAmount.Text = txtTotalAmount.Value;
        #endregion
    }
    void PolicyholdersListDataBind()
    {
        string[] fields = new string[] { "Name", "NameEn", "IDType", "IDNo", "Sex", "Birthday", "Age" };
        string[] fieldstype = new string[] { "System.String", "System.String", "System.String", "System.String", "System.String", "System.String", "System.String" };
        DataTable dat = (new DataTableUtils()).BuildingDTStruct("PolicyholdersDAT", fields, fieldstype);

        for (int i = 0; i <= txtIDNo.Value.Split(',').Length - 1; i++)
        {
            dat.Rows.Add(new object[] { 
                txtName.Value.Split(',')[i].Trim(), 
                txtNameEn.Value.Split(',')[i].Trim(), 
                BLL.Sys.EnumItem.GetTextByValue("IDType", txtIDType.Value.Split(',')[i].Trim()), 
                txtIDNo.Value.Split(',')[i].Trim(),
                txtSex.Value.Split(',')[i].Trim().Replace("1", "女").Replace("0", "男"),
                txtBirthday.Value.Split(',')[i].Trim(),
                txtAge.Value.Split(',')[i].Trim()
            });
        }
        if (dat != null)
        {
            txtPersonQuantity.Value = dat.Rows.Count.ToString();
            txtTotalAmount.Value = (dat.Rows.Count * decimal.Parse(txtPrice.Value)).ToString();
        }

        rptPolicyholders.DataSource = dat;
        rptPolicyholders.DataBind();
    }
    #endregion
    
    #region 获取订单信息
    Model.Insurance.InsurancePolicyBase PolicyObj
    {
        get
        {
            Model.Insurance.InsurancePolicyBase _obj = new Model.Insurance.InsurancePolicyBase();

            _obj.ProductItemId = txtProductItemId.Value.Trim();
            _obj.ProductId = txtProductId.Value.Trim();
            _obj.PassCountry = txtPassCountryCode.Value.Trim();
            _obj.DestCountryName = txtDestCountryCode.Value.Trim();
            _obj.Email = txtEmail.Value.Trim();

            _obj.TName = txtTName.Value.Trim();
            _obj.ETName = txtTNameEn.Value.Trim();
            _obj.TBirtyday = DateTime.Parse(string.IsNullOrEmpty(txtTBirthday.Value.Trim()) ? "1754-01-01": txtTBirthday.Value.Trim());
            _obj.TSex = txtTSex.Value.Trim();
            _obj.TIDType = txtTIDType.Value.Trim();
            _obj.TIDNo = txtTIDNo.Value.Trim();
            _obj.TAge = txtTAge.Value.Trim();
            _obj.Name = txtName.Value.Trim();
            _obj.EName = txtNameEn.Value.Trim();
            _obj.Birtyday = txtBirthday.Value.Trim();
            _obj.Sex = txtSex.Value.Trim();
            _obj.Age = txtAge.Value.Trim();
            _obj.IDType = txtIDType.Value.Trim();
            _obj.IDNo = txtIDNo.Value.Trim();
            _obj.Phone = txtPhone.Value.Trim();
            _obj.CJType = txtCJType.Value.Trim();

            _obj.GetStartDate = DateTime.Parse(txtStartDate.Value);
            _obj.EndDate = DateTime.Parse(txtEndDate.Value.Trim());
            _obj.SalesTypeCode = int.Parse(txtSalesTypeCode.Value);
            _obj.ExpressType = txtExpressType.Value.Trim();
            _obj.Invoice = (txtInvoice.Value == "1");
            _obj.InvoiceRecipient = txtInvoiceRecipient.Value.Trim();
            _obj.InvoicePhone = txtInvoicePhone.Value.Trim();
            _obj.InvoiceTitle = txtInvoiceTitle.Value.Trim();
            _obj.InvoiceAddress = txtInvoiceAddress.Value.Trim();
            _obj.InvoiceFee = decimal.Parse(txtInvoiceFee.Value.Trim());
            _obj.Price = decimal.Parse(txtPrice.Value.Trim());
            _obj.TotalAmount = decimal.Parse(txtTotalAmount.Value.Trim());
            _obj.PersonQuantity = int.Parse(txtPersonQuantity.Value.Trim());
            _obj.OrderDate = DateTime.Now;
            _obj.CreatedOn = DateTime.Now;
            _obj.ModifyOn = DateTime.Now;
            _obj.CancelDate = DateTime.MaxValue; 
            _obj.CustomerId = (new MemberPageBase()).UserId;

            #region 受益人
            //IList<Model.Insurance.InsuranceBeneficiaries> _litems = new List<Model.Insurance.InsuranceBeneficiaries>();
            //for (int i = 0; i <= txtESName.Value.Split(',').Length - 1; i++)
            //{
            //    _litems.Add(new Model.Insure.InsuranceBeneficiaries(
            //        "",
            //        txtRelation.Value.Split(',')[i].Trim(),
            //        txtSIDType.Value.Split(',')[i].Trim(),
            //        txtSName.Value.Split(',')[i].Trim(),
            //        txtESName.Value.Split(',')[i].Trim(),
            //        txtSIDNo.Value.Split(',')[i].Trim(),
            //        txtBnforder.Value.Split(',')[i].Trim(),
            //        txtBnfPortion.Value.Split(',')[i].Trim(),
            //        i + 1
            //    ));
            //}
            //_obj.BNFS = new Model.Insure.InsuranceBeneficiaries[_litems.Count];
            //_litems.CopyTo(_obj.BNFS, 0);
            #endregion

            return _obj;
        }
        set
        {
            if (value != null)
            {
                txtProductItemId.Value = value.ProductItemId;
                txtProductId.Value = value.ProductId;
                txtPassCountryCode.Value = value.PassCountry;
                txtDestCountryCode.Value = value.DestCountryName;
                txtPassCountry.Value = value.PassCountry;
                txtDestCountryCode.Value = value.DestCountryName;
                txtEmail.Value = value.Email;
                txtTName.Value = value.TName;
                txtTNameEn.Value = value.ETName;
                txtTBirthday.Value = value.TBirtyday.ToString("yyyy-MM-dd").Replace("1754-01-01", "");
                txtTSex.Value = value.TSex;
                txtTIDType.Value = value.TIDType;
                txtTIDNo.Value = value.TIDNo;
                txtTAge.Value = value.TAge;
                txtName.Value = value.Name;
                txtNameEn.Value = value.EName;
                txtBirthday.Value = value.Birtyday;
                txtSex.Value = value.Sex;
                txtIDType.Value = value.IDType;
                txtIDNo.Value = value.IDNo;
                txtAge.Value = value.Age;
                txtPhone.Value = value.Phone;
                txtCJType.Value = value.CJType;
                txtStartDate.Value = value.GetStartDate.ToString("yyyy-MM-dd");
                txtEndDate.Value = value.EndDate.ToString("yyyy-MM-dd");
                txtInvoice.Value = value.Invoice ? "1" : "0";
                txtInvoiceRecipient.Value = value.InvoiceRecipient;
                txtInvoicePhone.Value = value.InvoicePhone;
                txtInvoiceTitle.Value = value.InvoiceTitle;
                txtInvoiceAddress.Value = value.InvoiceAddress;
                txtInvoiceFee.Value = value.InvoiceFee.ToString();
                txtPrice.Value = value.Price.ToString(); 

                #region 受益人
                //foreach (Model.Insure.InsuranceBeneficiaries _item in value.BNFS)
                //{
                //    txtBnfPortion.Value += _item.BnfPortion + ",";
                //    txtBnforder.Value += _item.Bnforder + ",";
                //    txtSIDNo.Value += _item.SIDNo + ",";
                //    txtSIDType.Value += _item.SIDType + ",";
                //    txtRelation.Value += _item.Relation + ",";
                //    txtESName.Value += _item.ESName + ",";
                //    txtSName.Value += _item.SName + ",";
                //}
                //if (txtBnfPortion.Value.Trim() != "")
                //{
                //    txtBnfPortion.Value += "|";
                //    txtBnforder.Value += "|";
                //    txtSIDNo.Value += "|"; ;
                //    txtSIDType.Value += "|";
                //    txtRelation.Value += "|";
                //    txtESName.Value += "|";
                //    txtSName.Value += "|";

                //    txtBnfPortion.Value = txtBnfPortion.Value.Replace(",|", "");
                //    txtBnforder.Value = txtBnforder.Value.Replace(",|", "");
                //    txtSIDNo.Value = txtSIDNo.Value.Replace(",|", ""); ;
                //    txtSIDType.Value = txtSIDType.Value.Replace(",|", "");
                //    txtRelation.Value = txtRelation.Value.Replace(",|", ""); ;
                //    txtESName.Value = txtESName.Value.Replace(",|", "");
                //    txtSName.Value = txtSName.Value.Replace(",|", "");
                //}
                #endregion
            }
        }
    }
    #endregion

    #region 保存信息
    void SaveOrder(int otype)
    {
        if (String.IsNullOrEmpty(this.User.Identity.Name))
        { 
            divErrorMsg.Style.Add("display", "");
            divErrorMsg.InnerHtml = string.Format(divErrorMsg.InnerHtml, "<li style=\"background: none;\">请先<a href=\"membership_login_dialog.aspx\" id=\"aLogin\">登录</a>，或者您还不是会员点击<a href=\"membership_register_dialog.aspx\" id=\"aRegister\">快速注册</a>.</li>");
        }
        else
        {
            string result = BLL.Insurance.InsurancePolicyBase.InsurancePolicyAdmin(PolicyObj);
            if (result.Split('|')[0] == "succeeded")
            {
                if (otype == 1)
                    JScript.AlertAndRedirect("保存成功!", "insurance_order_list.aspx");
                else
                    Response.Redirect("insurance_orderpayment.aspx?objectid=" + HttpUtility.UrlEncode(result.Split('|')[1]));
            }
            else
            {
                divErrorMsg.Style.Add("display", "");
                divErrorMsg.InnerHtml = string.Format(divErrorMsg.InnerHtml, "<li style=\"background: none;\">" + result + "</li>");
            }
        }
    }
    #endregion
}